<template>
  <div class="org">
    <el-form ref="form" :model="form" label-width="80px" class="form">
      <div style="width: 600px;display: flex;">
        <el-form-item label="题库类型:">
          <el-select
            v-model="form.rule"
            style="width: 185px;"
            placeholder="网球规则"
            @change="ruleChange"
          >
            <el-option
              :label="item.cate_name"
              :value="item.id"
              v-for="(item, index) in getLibTypeArr"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-input
          style="width: 300px;margin-left: 40px;"
          v-model="form.keyword"
          @keyup.enter.native.prevent="onSubmit"
          placeholder="请输试题标题关键字搜索"
        ></el-input>
      </div>
      <div class="search">
        <el-button type="primary" @click="addColumn">新增试题</el-button>
      </div>
    </el-form>
    <div class="orgTable">
      <el-table :data="tableData" stripe style="width: 60%;">
        <el-table-column prop="question_item" label="题目">
          <template slot-scope="scope">
            <div
              style="width: 143px;white-space:nowrap;overflow: hidden;text-overflow: ellipsis;"
              v-html="scope.row.question_item"
            ></div>
          </template>
        </el-table-column>
        <el-table-column prop="c_cate_name" label="题库类型"></el-table-column>
        <el-table-column prop="b_cate_name" label="知识点"></el-table-column>
        <el-table-column prop="question_item" label="级别">
          <template slot-scope="scope">
            <span>{{scope.row.level == '1' ? '一' : '二'}}级</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <span class="operation" @click="modify(scope.row)">修改</span>
            <span>|</span>
            <span class="operation" @click="delete(scope.row)">删除</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      @current-change="ChangePage"
      style=" float: right;margin-bottom: 40px; margin-right: 150px;"
    ></el-pagination>
  </div>
</template>
<script>
export default {
  data() {
    return {
      tableData: [],
      getLibTypeArr: [],
      total: null,
      currentPage: "",
      dialogFormVisible: false,
      form: {
        keyword: "",
        rule: 0
      },
      dialogForm: {
        rule: 1,
        name: ""
      },
      type: null,
      title: ""
    };
  },
  created() {
    this.getTopic()
    this.getLibType()
  },
  methods: {
    ChangePage(page) {
      this.currentPage = page;
      this.getTopic();
    },
    onSubmit() {
      this.getTopic();
    },
    getLibType() {
      this.$axios
        .post(
          "/Qlib/getLibType",
          {},
          {
            headers: {
              token: window.sessionStorage.getItem("token")
            }
          }
        )
        .then(res => {
          const { code, data, msg } = res.data;
          if (code == 2000) {
            data.unshift({
              id: 0,
              cate_name: "全部"
            });
            this.getLibTypeArr = data;
          }
        })
        .catch(error => {});
    },
    getTopic() {
      this.$axios
        .post(
          "/Qlib/itemList",
          {
            keywords: this.form.keyword,
            lib_type: this.form.rule,
            limit: 10,
            page: this.currentPage
          },
          {
            headers: {
              token: window.sessionStorage.getItem("token")
            }
          }
        )
        .then(res => {
          const { code, data, msg } = res.data;
          if (code == 2000) {
            this.tableData = data.rows;
            this.total = data.count;
          } else {
            this.$message({
              type: "info",
              message: msg
            });
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    addColumn() {
      this.$router.push({
        path: `/index/questionMange/add_questions`,
        query: {
          id: "",
          name: "新增题库"
        }
      });
    },
    modify(row) {
      this.$router.push({
        path: `/index/questionMange/add_questions`,
        query: {
          id: row.id,
          name: "修改题库"
        }
      });
    },
    delete(item) {
      this.$confirm("确定删除该题目?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$axios
            .post(
              "/Qlib/delete_lib",
              {
                id: item.id
              },
              {
                headers: {
                  token: window.sessionStorage.getItem("token")
                }
              }
            )
            .then(res => {
              if (res.data.code == 2000) {
                this.tableData.splice(index, 1);
                this.$message({
                  message: res.data.msg,
                  type: "success"
                });
              } else {
                this.$message({
                  message: res.data.msg,
                  type: "error"
                });
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "您已取消"
          });
        });
    },
    ruleChange(val) {
      this.form.rule = val;
      this.getTopic();
    }
  }
};
</script>
<style lang="less" scoped>
.org {
  margin-top: 36px;

  .form {
    width: 70%;
    display: flex;
    justify-content: space-between;
    margin-left: 42px;
    margin-bottom: 35px;

    .keyword {
      width: 410px;
    }

    .state {
      width: 200px;
    }

    .search {
      width: 123px;
      text-align: right;
    }
  }

  .line {
    width: 80%;
    /*height: 0.5px;*/
    border-top: 1px #dcdfe6 dotted;
  }

  .orgTable {
    margin-top: 34px;
    margin-left: 40px;
  }
}

.ogg {
  color: #ff9800;
}

.green {
  color: #259b24;
}

.red {
  color: red;
}

.operation {
  color: #409eff;
  cursor: pointer;
}
</style>
